<script setup lang="ts" name="PaneExternalTask">
import { mapState } from 'pinia'
import { useModelerStore } from '@/store/bpmnProcess/modelerStore'
import EventEmitter from '@/utils/EventEmitter'
import { isExternalTask, getExternalTaskValue, setExternalTaskValue } from '../../utils/externalTask'

const mapStates = computed(() => {
  return mapState(useModelerStore, ['getActive'])
})

const taskPriority = ref('')
const getTaskPriority = () => {
  taskPriority.value = getExternalTaskValue(mapStates.value.getActive()) || ''
}
const setTaskPriority = (v) => {
  setExternalTaskValue(mapStates.value.getActive(), v)
}

const eventEmitterListener = () => {
  if (isExternalTask(mapStates.value.getActive())) {
    getTaskPriority()
  }
}

onBeforeUnmount(() => {
  EventEmitter.removeListener('element-update', eventEmitterListener)
})

onMounted(() => {
  getTaskPriority()
  EventEmitter.on('element-update', eventEmitterListener)
})
</script>

<template>
  <el-collapse-item name="external-task">
    <template #title>
      <div class="collapse-header">
        <svg-icon icon="ri:todo-fill"></svg-icon>
        <h2>外部任务</h2>
      </div>
    </template>
    <el-form-item label="优先级">
      <el-input v-model="taskPriority" maxlength="20" @change="setTaskPriority" />
    </el-form-item>
  </el-collapse-item>
</template>

<style scoped lang="scss">
.el-collapse-item {
  :deep(.el-collapse-item__header) {

    .collapse-header {
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0 12px;

      .el-icon {
        margin-right: 6px;
        font-size: 20px !important;
      }

      h2 {
        font-weight: bold;
        font-size: 14px;
      }
    }

    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-collapse-item__content) {
    padding-right: 12px;
    padding-bottom: 12px;
    padding-left: 12px;
  }
}
</style>
